package com.oa.ylms.apps.actions.goods;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.goods.IGoodsCategoryService;
import com.oa.ylms.apps.business.goods.IGoodsInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.goods.IGoodsInfoStates;
import com.oa.ylms.apps.entity.goods.GoodCategoryValue;
import com.oa.ylms.apps.entity.goods.GoodInfoValue;

@SuppressWarnings("serial")
public class GoodsInfoAction extends BasicAction {
	private IGoodsInfoService infoService;
	private IGoodsCategoryService cateService;

	private List<GoodInfoValue> list;
	private List<GoodCategoryValue> cateList;
	private List<GoodCategoryValue> allCateList;
	private GoodInfoValue goodInfo;
	private GoodCategoryValue goodCategory;
	
	private String goodInfoJsonString;
	
	private Integer categoryId=-1;
	private Integer goodId; 
	private boolean isSuccess;
	private Integer states=-1;
	
	
	public String execute() {
		try { 
			totalpage=infoService.countTpages(categoryId, states, rows);
			count=infoService.getTotalPages();
			if(count%rows==0&&totalpage+1==currpage){
				currpage=totalpage;
			}
			if(goodId==null){
				currpage=1;
			}
			list= infoService.queryWithPage(categoryId, states, rows, currpage);
			cateList = cateService.queryAll(IDEDStates.ENABLE);
			allCateList=cateService.queryAll(IGoodsInfoStates.NOTBYSTATES);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "show";
	}
	public String justShow(){
		try {
			allCateList=cateService.queryAll(IGoodsInfoStates.NOTBYSTATES);
			cateList = cateService.queryAll(IDEDStates.ENABLE);
			list=infoService.queryWithPage(categoryId, states, rows, currpage);
			totalpage=infoService.countTpages(categoryId, states, rows);
			count=infoService.queryWithPage(categoryId, states, -1, currpage).size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "justShow";
	}

	
	public String update() {
		try {
			isSuccess = infoService.modifyGoodsInfo(goodInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "update";
	}

	public String delete() {
		System.out.println("currentPage:"+currpage);
		try {
			isSuccess = infoService.deleteGoodsInfo(goodId);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}
	public String queryInfo(){
		try {
			if(categoryId!=-1){
			list=infoService.queryWithPage(categoryId, null, rows, currpage);
			totalpage=infoService.countTpages(categoryId, null, rows);
			}else{
				list= infoService.queryWithPage(null, null, rows, currpage);
				totalpage=infoService.countTpages(null, null, rows);
			}
			System.out.println(list.get(0).getGoodName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "queryInfo";
	}
	
	public String add() {
		try {
		JSONObject json =   JSONObject.fromObject(goodInfoJsonString);
		if(!json.get("goodId").equals("")){
		goodInfo.setGoodId(json.getInt("goodId"));
		}
		if(!json.get("goodsName").equals("")){
		goodInfo.setGoodName(json.getString("goodsName"));;
		}
		if(!json.get("description").equals("")){
		goodInfo.setDescription(json.getString("description"));
		}
		if(!json.get("categoryId").equals("")){
		goodInfo.setGoodCategory(cateService.queryByCateId(json.getInt("categoryId")));
		}
		if(!json.get("price").equals("")){
		goodInfo.setPrice(json.getDouble("price"));
		}
		if(!json.get("total").equals("")){
		goodInfo.setTotal(json.getInt("total"));
		}
			if(goodInfo.getGoodId()==null){
			infoService.addGoodsInfo(goodInfo);
			currpage=-1;
			}else{ 
				infoService.modifyGoodsInfo(goodInfo);
			}
		
			//response.sendRedirect(location)
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public String addCategory() throws Exception{
		String name=null;
		JSONObject json =   JSONObject.fromObject(goodInfoJsonString);
		if(!json.get("categoryName").equals("")){
		 name=json.getString("categoryName");
		}
		cateService.addGoodsCategory(name);
		return null;
	}
	public String getGoodsByCategory() {
		List<GoodInfoValue> list = null;
		try {
			list = infoService.queryAllByCateId(categoryId,
					IGoodsInfoStates.FORMANAGER);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		JSONArray obj = JSONArray.fromObject(list);
		String str = obj.toString();
		response.setContentType("text/html; charset=gb2312");
		response.setHeader("Cache-Control", "no-cache");
		try {
			response.getWriter().print(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String queryByDate() {

		return "queryByDate";
	}

	public String queryByName() {

		return "queryByName";
	}
	public IGoodsInfoService getInfoService() {
		return infoService;
	}
	public void setInfoService(IGoodsInfoService infoService) {
		this.infoService = infoService;
	}
	public IGoodsCategoryService getCateService() {
		return cateService;
	}
	public void setCateService(IGoodsCategoryService cateService) {
		this.cateService = cateService;
	}
	public List<GoodInfoValue> getList() {
		return list;
	}
	public void setList(List<GoodInfoValue> list) {
		this.list = list;
	}
	public List<GoodCategoryValue> getCateList() {
		return cateList;
	}
	public void setCateList(List<GoodCategoryValue> cateList) {
		this.cateList = cateList;
	}
	public List<GoodCategoryValue> getAllCateList() {
		return allCateList;
	}
	public void setAllCateList(List<GoodCategoryValue> allCateList) {
		this.allCateList = allCateList;
	}
	public GoodInfoValue getGoodInfo() {
		return goodInfo;
	}
	public void setGoodInfo(GoodInfoValue goodInfo) {
		this.goodInfo = goodInfo;
	}
	public GoodCategoryValue getGoodCategory() {
		return goodCategory;
	}
	public void setGoodCategory(GoodCategoryValue goodCategory) {
		this.goodCategory = goodCategory;
	}
	public String getGoodInfoJsonString() {
		return goodInfoJsonString;
	}
	public void setGoodInfoJsonString(String goodInfoJsonString) {
		this.goodInfoJsonString = goodInfoJsonString;
	}
	public Integer getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}
	public Integer getGoodId() {
		return goodId;
	}
	public void setGoodId(Integer goodId) {
		this.goodId = goodId;
	}
	public boolean isSuccess() {
		return isSuccess;
	}
	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}
	public Integer getStates() {
		return states;
	}
	public void setStates(Integer states) {
		this.states = states;
	}

	public String toAdd() {
		try {
			cateList = cateService.queryAll(IDEDStates.ENABLE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toAdd";
	}

	public String toUpdate() {
		try {
			goodInfo = infoService.queryByGoodId(goodId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toUpdate";
	}

}
